﻿using System;
//using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TE3;
using TE3.Entities;
using TE3.Utils;
namespace TileEngine3
{
	internal delegate void ConsoleCommand(string parameters);
	public class TileEngine3Game : TE3Game
	{

		public TileEngine3Game()
		{
			TE3Init();
			Content.RootDirectory = "Content";
		}
		protected override void Initialize()
		{
			base.Initialize();
			CurrentMap = new Map(new Int2(20,20),1, new Int2(32,32));
			//CurrentMap.MapData[0,0,0] = new TileInfo("Tiles/grass.png");
			//CurrentMap.FillRect(new Int2(5,5),new Int2(10,10),0, new TileInfo("Tiles/grass.png"));
			DrawManager.Drawables.Add("world", CurrentMap);
			Graphics.PreferredBackBufferWidth = CurrentMap.NeededSize.X;
			Graphics.PreferredBackBufferHeight = CurrentMap.NeededSize.Y;
			Graphics.ApplyChanges();

			var boxxy = new Box { FollowMouse = true };
			var window = new Window("TestWindow", new Rectangle(10, 10, 500, 100), this, new Color(255, 255, 255));
			EntityManager.AddEntity(boxxy, "MouseCursor", true, true);

			Console.WriteLine("TE3 Initialized!");
		}
		protected override void LoadContent()
		{
			SpriteBatch = new SpriteBatch(GraphicsDevice);
			Console.WriteLine("Loading content...");
			Console.WriteLine("Loading textures...");
			TextureHandler.LoadTextures();
			Console.WriteLine("Textures loaded.");
			Console.WriteLine("Loading fonts...");
			FontManager.LoadFont("Fonts/Console");
			Console.WriteLine("Fonts loaded.");
			Console.WriteLine("Content loaded.");
		}
		protected override void UnloadContent()
		{
			
		}
		protected override void Update(GameTime gameTime)
		{
			if (IsActive == false)
				return;
			if (Keyboard.GetState().IsKeyDown(Keys.Escape))
				Exit();
			if(Keyboard.GetState().IsKeyDown(Keys.F12))
				InputInConsole();
			if (Keyboard.GetState().IsKeyDown(Keys.Down))
			{
				WindowManager.Windows["TestWindow"].Position.Height++;
				WindowManager.Windows["TestWindow"].CalculatePosition();
			}
			if (Keyboard.GetState().IsKeyDown(Keys.Up))
			{
				WindowManager.Windows["TestWindow"].Position.Y++;
				WindowManager.Windows["TestWindow"].CalculatePosition();
			}
			if (Keyboard.GetState().IsKeyDown(Keys.F11))
				ConsoleCommands.RunLastCommand();
			if(Mouse.GetState().LeftButton == ButtonState.Pressed)
			{
				var x = TE3Math.ClampInt(Mouse.GetState().X / CurrentMap.TileSize.X, 0, CurrentMap.MapSize.X-1);
				var y = TE3Math.ClampInt(Mouse.GetState().Y / CurrentMap.TileSize.Y, 0, CurrentMap.MapSize.Y - 1);
				CurrentMap.MapData[x, y, 0] = new TileInfo("Tiles/grass.png");
			}
			foreach(var thinkable in ThinkableList)
				thinkable.Think(gameTime);
			base.Update(gameTime);
		}
		void InputInConsole()
		{
			ConsoleCommands.ConsoleWindow.Hidden = false;
			Console.Write("\t>");
// ReSharper disable PossibleNullReferenceException
			var consoleLine = Console.ReadLine().Split(';');
// ReSharper restore PossibleNullReferenceException
			if (!String.IsNullOrWhiteSpace(consoleLine[0]))
			{
				foreach (
					var paramLine in
						consoleLine.Select(command => command.Split(new[] {", "}, StringSplitOptions.None)).Where(
							paramLine => ConsoleCommands.RunCommand(paramLine[0], paramLine) == false))
				{
					Console.WriteLine("Invalid command: " + paramLine[0]);
				}
			}
			ConsoleCommands.ConsoleWindow.Hidden = true;
		}
		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);
			SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default,
			                  RasterizerState.CullNone);
			DrawManager.DrawDrawables(SpriteBatch,GraphicsDevice,TextureHandler);
			SpriteBatch.End();
			base.Draw(gameTime);
		}
	}
}
